package com.kpmg.datalake.web.config;

import com.kpmg.datalake.common.enums.ResultCodeEnum;
import com.kpmg.datalake.common.exception.BusinessException;
import com.kpmg.datalake.common.vo.ServerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 全局异常处理
 *
 * @author Alex.zhao [alex.zhao@kpmg.com]
 * @since Created on azhao6 on 2018-06-27 13:57:58
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<ServerResponse<String>> businessException(BusinessException ex) {
        logger.error(ex.getMessage());
        return new ResponseEntity<>(ServerResponse.createByErrorCodeMessage(ex.getResultCodeEnum(), ex.getLocalizedMessage()), HttpStatus.OK);

    }


    @ExceptionHandler(Exception.class)
    public ResponseEntity<ServerResponse<String>> unknownException(Exception ex) {
      logger.error(ex.getMessage(), ex);
        return new ResponseEntity<>(ServerResponse.createByErrorCodeMessage(ResultCodeEnum.CODE9998.getCode(), ex.getLocalizedMessage()), HttpStatus.OK);
    }
}
